import { AssetInfo } from '../../@types/packages/asset-db/@types/public';
import { exeCommand, EXTENSION_PATH } from './utils';
import fs from 'fs';
import os from 'os';

export function onAssetMenu(assetInfo: AssetInfo) {
    let isBundle = false;
    if (assetInfo.isDirectory) {
        let metafile = assetInfo.file + '.meta';
        if (fs.existsSync(metafile)) {
            let meta = JSON.parse(fs.readFileSync(metafile, { encoding: 'utf-8' }));
            if (meta.userData?.isBundle) {
                isBundle = true;
                return [
                    {
                        label: 'i18n:lcc-framework.menu-exprot-package-declaration',
                        async click() {
                            console.log('导出包声明...');
                            if (os.platform() === 'win32') {
                                let command = `npx gulp buildDeclaration --path "${assetInfo.file}"`;
                                let result = await exeCommand(command, EXTENSION_PATH);
                                if (result.error) {
                                    console.error(`error :\n${result.error}`);
                                } else {
                                    console.log(`stdout :\n${result.stdout}`);
                                    if (result.stderr) {
                                        console.error(`stderr :\n${result.stderr}`);
                                    }
                                }
                            }
                            console.log('导出包声明完成');
                        }
                    }
                ];
            }
        }
    }
}
